package gxwl.common.page;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.Min;

/**
 * 分页请求参数
 * @author jiangwh
 */
@Slf4j
@Validated
@Getter @Setter
@ToString
public class PageCondition {

    @Parameter(description = "当前页码，从1开始", required = true, schema = @Schema(defaultValue = "1"))
    @Min(value = 1, message = "pageNumber必须大于等于1")
    private Long pageNumber = 1L;

    @Parameter(description = "每页条数", required = true, schema = @Schema(defaultValue = "10"))
    @Min(value = 1, message = "pageSize必须大于等于1")
    private Long pageSize = 10L;

    /**
     * 将数据转换成数据库的分页对象
     * @param clazz 分页对象类
     * @return 数据库分页对象
     * @param <T> 分页对象类型
     */
    public <T> Page<T> toPage(Class<T> clazz) {
        Assert.notNull(clazz, "必须指定Page中的数据类型");
        return new Page<>(pageNumber, pageSize);
    }

}

